package com.eascs.crm.nsEditDetail.service;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.common.util.StringUtil;
import com.eascs.crm.nsEditDetail.dao.NsEditDetailDao;
import com.eascs.crm.nsEditDetail.entity.HouseInfoEntity;
import com.eascs.crm.nsEditDetail.entity.HouseInfoLogEntity;
import com.eascs.crm.nsEditDetail.form.HouseInfoForm;
import com.eascs.web.sso.UserHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import java.util.Date;

@Service
public class HouseInfoService {

	@Inject
	NsEditDetailDao nsEditDetailDao;

	public PageResult<Object[]> findHouseListInfo(String loanapplyId,Integer showPage,String type) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, 10, showPage);
		StringBuilder sql = new StringBuilder();
		
		if ("detail".equals(type)) {
			sql.append("select h.id,h.address AS address,h.aypes,h.is_mortgage, log.valuation,h.house_Price,log.house_Price house_price_review  from house_info h  left join house_info_log log on h.id =log.house_info_id  and log.op_date ");
			sql.append( " in (select max(ll.op_date) from house_info_log ll where ll.house_info_id=log.house_info_id) where 1=1 and h.is_del = 'N' ");
			
		}else{
			sql.append("select h.id,CONCAT( LEFT (h.address, 6), '******') AS address,h.aypes,h.is_mortgage, log.valuation,h.house_Price,log.house_Price house_price_review  from house_info h  left join house_info_log log on h.id =log.house_info_id  and log.op_date ");
			sql.append( " in (select max(ll.op_date) from house_info_log ll where ll.house_info_id=log.house_info_id) where 1=1 and h.is_del = 'N' ");
			
		}
		
		if (StringUtil.isNotBlank(loanapplyId)) {
			sql.append(" and h.loanapply_no = ?");
			sqlPageQuery.addParam(loanapplyId);
		}
		sqlPageQuery.setQuerySql(sql.toString());
		PageResult<Object[]> housePage = nsEditDetailDao.findDataWithPage(sqlPageQuery);
		return housePage;
	}
	
	/**
	 * @Description:
	 *               <p>
	 *               夫妻双方房产信息--脱敏（待办可见、已办脱敏）
	 *               </p>
	 * @author LMX
	 */
	public PageResult<Object[]> findHouseListInfo(String loanapplyId,Integer showPage,String type, String approvalStatus) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, 10, showPage);
		StringBuilder sql = new StringBuilder();
		
		if(StringUtil.isNotBlank(approvalStatus)){
			if (!"UNDO".equals(approvalStatus)) {
				sql.append("select h.id,CONCAT( '******', RIGHT (h.address, 6)) AS address,h.aypes,h.is_mortgage, log.valuation,h.house_Price,log.house_Price house_price_review  from house_info h  left join house_info_log log on h.id =log.house_info_id  and log.op_date ");
			    sql.append( " in (select max(ll.op_date) from house_info_log ll where ll.house_info_id=log.house_info_id) where 1=1 and h.is_del = 'N' ");
			}else {
				sql.append("select h.id,h.address AS address,h.aypes,h.is_mortgage, log.valuation,h.house_Price,log.house_Price house_price_review  from house_info h  left join house_info_log log on h.id =log.house_info_id  and log.op_date ");
			    sql.append( " in (select max(ll.op_date) from house_info_log ll where ll.house_info_id=log.house_info_id) where 1=1 and h.is_del = 'N' ");
			}
		}else {
			if (!"detail".equals(type)) {
				sql.append("select h.id,CONCAT( '******', RIGHT (h.address, 6) ) AS address,h.aypes,h.is_mortgage, log.valuation,h.house_Price,log.house_Price house_price_review  from house_info h  left join house_info_log log on h.id =log.house_info_id  and log.op_date ");
			    sql.append( " in (select max(ll.op_date) from house_info_log ll where ll.house_info_id=log.house_info_id) where 1=1 and h.is_del = 'N' ");
			}else{
				sql.append("select h.id,h.address AS address,h.aypes,h.is_mortgage, log.valuation,h.house_Price,log.house_Price house_price_review  from house_info h  left join house_info_log log on h.id =log.house_info_id  and log.op_date ");
			    sql.append( " in (select max(ll.op_date) from house_info_log ll where ll.house_info_id=log.house_info_id) where 1=1 and h.is_del = 'N' ");
			}
		}
	    
	    if (StringUtil.isNotBlank(loanapplyId)) {
			sql.append(" and h.loanapply_no = ?");
			sqlPageQuery.addParam(loanapplyId);
		}
		sqlPageQuery.setQuerySql(sql.toString());
		PageResult<Object[]> housePage = nsEditDetailDao.findDataWithPage(sqlPageQuery);
		return housePage;
	}
	
	public HouseInfoEntity findHouseInfoEntity(String houseId) {
		return nsEditDetailDao.getById(HouseInfoEntity.class, houseId);
	}
	
	public HouseInfoLogEntity findHouseLogEntity(String houseId) {
		return nsEditDetailDao.findEntity("select * from house_info_log l where l.op_date in (select MAX(t.op_date) from house_info_log t where t.house_info_id =l.house_info_id   group by t.house_info_id ) and l.house_info_id = ? ",
				HouseInfoLogEntity.class, houseId);
	}
	
	public HouseInfoEntity findHouseInfoByLoanapplyId(String loanapplyId) {
		return nsEditDetailDao.findEntity("select * from house_info where loanapply_no = ?",
				HouseInfoEntity.class, loanapplyId);
	}

	
	@Transactional(transactionManager="o2oTransactionManager",rollbackFor=Exception.class)
	public boolean houseDel(String houseId){
		nsEditDetailDao.execute("update  house_info set is_del ='Y' where id = ?", houseId);
		return true;
	}
	
	@Transactional(transactionManager="o2oTransactionManager",rollbackFor=Exception.class)
	public boolean houseSave(HouseInfoEntity house,HouseInfoForm form) {
		setHousePorperty(house, form);
		nsEditDetailDao.insert(house);
		saveLog(house);
		return  true;
	}
	
	@Transactional(transactionManager="o2oTransactionManager",rollbackFor=Exception.class)
	public boolean houseUpdate(HouseInfoEntity house,HouseInfoForm form) {
		setHousePorperty(house, form);
		nsEditDetailDao.updateById(house);
		saveLog(house);
		return  true;
	}

	private void saveLog(HouseInfoEntity house) {
		HouseInfoLogEntity log= new HouseInfoLogEntity();
		log.setHouseInfoId(house.getId());
		log.setOpPsn(UserHolder.getUser().getCname());
		log.setOpNo(UserHolder.getUser().getCode());
		log.setOpDate(new Date());
		log.setValuation(house.getValuation());
		log.setHousePrice(house.getHousePrice());
		nsEditDetailDao.insert(log);
	}

	private void setHousePorperty(HouseInfoEntity house, HouseInfoForm form) {
		house.setAddress(form.getAddress());
		house.setAypes(form.getAypes());
		house.setIsMortgage(form.getIsMortgage());
		house.setValuation(form.getValuation());
		house.setHousePrice(form.getHousePrice());
		house.setProvince(form.getProvince());
		house.setCounty(form.getCounty());
		house.setCity(form.getCity());
		house.setIsDel("N");
		house.setLoanapplyNo(StringUtil.isBlank(form.getLoanapplyId())?house.getLoanapplyNo():form.getLoanapplyId());
		house.setId(form.getHouseId());
	}


	public <T> int insert(T entity) {
		return nsEditDetailDao.insert(entity);
	}
	
	
	public boolean checkHouse(String loanapplyId){
		String sql="SELECT count(*) FROM house_info l INNER JOIN house_info_log log ON l.id = log.house_info_id WHERE log.op_date in (select MAX(t.op_date) from house_info_log t where t.house_info_id =log.house_info_id   group by t.house_info_id ) and  l.loanapply_no = ? and log.house_price is  null ";
		int i=nsEditDetailDao.findInteger(sql,loanapplyId);
		if(i>0){
			return true;
		}else{
			return false;
		}
	}
}
